﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using YArchitech.DataModel;
using YArchitech.HVAC;
using YArchitech.LIB;
using YArchitech.MEP;
using YArchitech.Plumbing.Model;
using YArchitech.ProgressBar;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	internal class SprinklerModelProcess
	{
		public SprinklerModelProcess(ExternalCommandData commandData, YJKWindowHandle wndRevit)
		{
			this.uiApp = commandData.Application;
			this.doc = commandData.Application.ActiveUIDocument.Document;
			this._wndRevit = wndRevit;
			this.rsh = new RecSprinklerHelper(this.doc);
			this.lph = new LaySprinklerHelper(this.doc);
			this.floorrang = YJKRevitTools.GetFloorHeigh(this.doc, commandData.Application.ActiveUIDocument.ActiveView);
		}

		public bool Go(FamilySymbol fs, FormLaySprinklerByRectangle dlg, List<XYZ> xyzList, XYZ sMin, XYZ sMax, XYZ sEnd, bool blRectLay)
		{
			ProgressBarForm progressBarForm = new ProgressBarForm(9, ProgressBarStyle.Blocks, true);
			bool result;
			try
			{
				progressBarForm.Text = "喷头布置";
				progressBarForm.ShowInTaskbar = false;
				progressBarForm.Show(this._wndRevit);
				this.layCom = SprinklerModelProcess.LaySprinklerType.Lay_Rect;
				this.familySymbol = fs;
				this.listPoint = xyzList;
				this.startMin = new XYZ(sMin.X, sMin.Y, 0.0);
				this.startMax = new XYZ(sMax.X, sMax.Y, 0.0);
				this.startEnd = new XYZ(sEnd.X, sEnd.Y, 0.0);
				this.fromRectangle = dlg;
				result = this.Process(progressBarForm, blRectLay);
			}
			catch
			{
				result = false;
			}
			finally
			{
				progressBarForm.Dispose();
			}
			return result;
		}

		public bool Go(FamilySymbol fs, LayBasementManage lay, bool basementLay)
		{
			ProgressBarForm progressBarForm = new ProgressBarForm(9, ProgressBarStyle.Blocks, true);
			bool result;
			try
			{
				progressBarForm.Text = "喷头布置";
				progressBarForm.ShowInTaskbar = false;
				progressBarForm.Show(this._wndRevit);
				this.layCom = SprinklerModelProcess.LaySprinklerType.Lay_Base;
				this.familySymbol = fs;
				this.listPoint = lay.listPoints;
				this.startMin = new XYZ(lay.startMin.X, lay.startMin.Y, 0.0);
				this.startMax = new XYZ(lay.startMax.X, lay.startMax.Y, 0.0);
				this.startEnd = new XYZ(lay.startEnd.X, lay.startEnd.Y, 0.0);
				this.basementManage = lay;
				bool flag = this.Process(progressBarForm, basementLay);
				progressBarForm.Dispose();
				result = flag;
			}
			catch
			{
				progressBarForm.Dispose();
				result = false;
			}
			return result;
		}

		private bool Process(ProgressBarForm progressBar, bool bl)
		{
			ICollection<ElementId> allFamilyInstance = this.equipmentCM.GetAllFamilyInstance(this.doc, BuiltInCategory.OST_Sprinklers);
			bool flag = false;
			this.startMin = new XYZ(this.startMin.X, this.startMin.Y, 0.0);
			this.startMax = new XYZ(this.startMax.X, this.startMax.Y, 0.0);
			this.startEnd = new XYZ(this.startEnd.X, this.startEnd.Y, 0.0);
			Transaction transaction = null;
			bool result;
			try
			{
				progressBar.Text = "喷头布置(识别梁窝)";
				progressBar.RefreshProgressBar();
				if (progressBar.m_stopTag)
				{
					result = false;
				}
				else
				{
					if (((this.layCom == SprinklerModelProcess.LaySprinklerType.Lay_Rect && this.fromRectangle.isBeams) || this.layCom == SprinklerModelProcess.LaySprinklerType.Lay_Base) && !bl)
					{
						flag = this.LiangGrid();
					}
					if (progressBar.m_stopTag)
					{
						result = false;
					}
					else
					{
						progressBar.Text = "喷头布置(布置喷头)";
						progressBar.RefreshProgressBar();
						transaction = new Transaction(this.doc, "喷头批量布置");
						transaction.Start();
						if (!this.LaySprinkler())
						{
							transaction.RollBack();
							result = false;
						}
						else if (progressBar.m_stopTag)
						{
							result = false;
						}
						else
						{
							progressBar.Text = "喷头布置(添加参数)";
							progressBar.RefreshProgressBar();
							ICollection<ElementId> allFamilyInstance2 = this.equipmentCM.GetAllFamilyInstance(this.doc, BuiltInCategory.OST_Sprinklers);
							IEnumerable<ElementId> enumerable = new List<ElementId>();
							enumerable = allFamilyInstance.Intersect(allFamilyInstance2);
							if (enumerable.Count<ElementId>() > 0)
							{
								foreach (ElementId item in enumerable)
								{
									allFamilyInstance2.Remove(item);
								}
							}
							List<List<Element>> list = new List<List<Element>>();
							if (bl)
							{
								list = this.AddParameter(allFamilyInstance2, true);
							}
							else
							{
								list = this.AddParameter(allFamilyInstance2, false);
							}
							if (progressBar.m_stopTag)
							{
								result = false;
							}
							else
							{
								progressBar.Text = "喷头布置(模型分类)";
								progressBar.RefreshProgressBar();
								List<SprinklerGroup> list2 = new List<SprinklerGroup>();
								foreach (List<Element> sprinkers in list)
								{
									SprinklerGroup sprinklerGroup = new SprinklerGroup(sprinkers, this.uiApp.ActiveUIDocument);
									if (sprinklerGroup.systemLine != null && sprinklerGroup.allElements.Count > 0)
									{
										list2.Add(sprinklerGroup);
									}
								}
								if (progressBar.m_stopTag)
								{
									result = false;
								}
								else
								{
									progressBar.RefreshProgressBar();
									if (bl)
									{
										flag = false;
									}
									if ((this.layCom == SprinklerModelProcess.LaySprinklerType.Lay_Rect && this.fromRectangle.isParking && !flag) || this.layCom == SprinklerModelProcess.LaySprinklerType.Lay_Base)
									{
										progressBar.Text = "喷头布置(车位分析)";
										this.ParkingIdent(list2);
									}
									if (progressBar.m_stopTag)
									{
										result = false;
									}
									else
									{
										progressBar.RefreshProgressBar();
										if (this.layCom == SprinklerModelProcess.LaySprinklerType.Lay_Base)
										{
											progressBar.Text = "喷头布置(梁避让)";
											this.BeamAside(list2);
										}
										if (progressBar.m_stopTag)
										{
											result = false;
										}
										else
										{
											progressBar.RefreshProgressBar();
											if ((this.layCom == SprinklerModelProcess.LaySprinklerType.Lay_Rect && this.fromRectangle.isAvoidColumns) || this.layCom == SprinklerModelProcess.LaySprinklerType.Lay_Base)
											{
												progressBar.Text = "喷头布置(柱避让)";
												this.PillarAside(list2);
											}
											progressBar.RefreshProgressBar();
											transaction.Commit();
											result = true;
										}
									}
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				result = false;
			}
			finally
			{
				if (transaction != null && (int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
			}
			return result;
		}

		private bool LiangGrid()
		{
			bool result = false;
			RectangleRange rectangleRange = new RectangleRange(this.startMin, this.startMax, this.startEnd, this.startMin + (this.startEnd - this.startMax));
			if ((this.floorrang.floorM == YJKViewRange.FloorMark.MidFloor && Math.Abs(this.floorrang.height * 304.8) < 8000.0) || this.floorrang.floorM == YJKViewRange.FloorMark.TopFloor)
			{
				double elevation = this.uiApp.ActiveUIDocument.ActiveView.GenLevel.Elevation;
				List<BeamModel> allBeams = this.rsh.GetAllBeams(this.floorrang, elevation);
				List<Line> lines = Algorithm.CombineCollineationLines(this.rsh.GetLinesByAllBeams(allBeams), 1.0);
				List<Line> rangedLines = this.rsh.GetRangedLines(rectangleRange, lines);
				if (rangedLines.Count >= 1)
				{
					int count = this.rsh.CutRectangleWithLines(rangedLines, rectangleRange).Count;
				}
			}
			return result;
		}

		private bool LaySprinkler()
		{
			ConnectHelper connectHelper = new ConnectHelper();
			if (this.listPoint != null && this.listPoint.Count > 0)
			{
				ElementSet elementSet = connectHelper.LayFamilyInstance(this.listPoint, this.familySymbol, this.doc.ActiveView.GenLevel, 0, this.doc);
				this.doc.Regenerate();
				ConnectHelper connectHelper2 = new ConnectHelper();
				if (elementSet == null || elementSet.IsEmpty)
				{
					return false;
				}
				IEnumerator enumerator = elementSet.GetEnumerator();
				{
					while (enumerator.MoveNext())
					{
						object obj = enumerator.Current;
						XYZ origin = connectHelper2.GetYJKElementConnector(obj as Element).Origin;
						XYZ xyz = new XYZ(0.0, 0.0, 1.0);
						Line line = Line.CreateBound(origin, origin.Add(xyz * 1.0));
						ElementTransformUtils.RotateElement(this.doc, (obj as Element).Id, line, 0.0);
					}
					goto IL_122;
				}
			}
			MessageBox.Show("布置失败", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
			IL_122:
			this.doc.Regenerate();
			return true;
		}

		private List<List<Element>> AddParameter(ICollection<ElementId> collectionId1, bool bl)
		{
			new AuxiliaryLineOperate();
			List<Element> list = new List<Element>();
			try
			{
				foreach (ElementId elementId in collectionId1)
				{
					Element elementById = this.doc.GetElementById(elementId);
					list.Add(elementById);
					HYParametersSetting hyparametersSetting = new HYParametersSetting(this.uiApp);
					Parameter parameter = elementById.GetParameter(BuiltInParameter.FAMILY_LEVEL_PARAM);
					if (parameter != null)
					{
						parameter.Set(this.doc.ActiveView.GenLevel.Id);
					}
					if (this.layCom == SprinklerModelProcess.LaySprinklerType.Lay_Rect)
					{
						Parameter parameter2 = (elementById as FamilyInstance).Symbol.GetParameter(BuiltInParameter.RBS_FP_SPRINKLER_K_FACTOR_PARAM);
						if (parameter2 != null)
						{
							parameter2.Set(this.fromRectangle.sprinklersPara.Kratio);
						}
						Parameter parameter3 = elementById.GetParameter("喷淋半径");
						if (parameter3 == null)
						{
							hyparametersSetting.SetSprinklersRudioShareParam();
							parameter3 = elementById.GetParameter("喷淋半径");
						}
						if (parameter3 != null)
						{
							parameter3.Set(UnitConvert.CovertToAPI(this.fromRectangle.sprinklersPara.Radius * 1000.0, (Autodesk.Revit.DB.DisplayUnitType)2));
						}
						Parameter parameter4 = elementById.GetParameter("危险等级");
						if (parameter4 == null)
						{
							hyparametersSetting.SetSprinklersDangerShareParam();
							parameter4 = elementById.GetParameter("危险等级");
						}
						if (parameter4 != null)
						{
							parameter4.Set(this.fromRectangle.sprinklersPara.Danger);
						}
						Parameter parameter5 = elementById.GetParameter("行间距");
						if (parameter5 == null)
						{
							hyparametersSetting.SetSprinklersRowDistanceShareParam();
							parameter5 = elementById.GetParameter("行间距");
						}
						if (parameter5 != null)
						{
							parameter5.Set(UnitConvert.CovertToAPI(this.fromRectangle.sprinklersPara.RowDistance, (Autodesk.Revit.DB.DisplayUnitType)2));
						}
						Parameter parameter6 = elementById.GetParameter("列间距");
						if (parameter6 == null)
						{
							hyparametersSetting.SetSprinklersColDistanceShareParam();
							parameter6 = elementById.GetParameter("列间距");
						}
						if (parameter6 != null)
						{
							parameter6.Set(UnitConvert.CovertToAPI(this.fromRectangle.sprinklersPara.ColDistance, (Autodesk.Revit.DB.DisplayUnitType)2));
						}
						Parameter parameter7 = elementById.GetParameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM);
						if (this.fromRectangle.CurrentBtn == 0 && this.fromRectangle.familyCategory == 5)
						{
							LaySprinklerHelper.LayUpDownSprinkler(this.doc, elementById, this.fromRectangle.layTerminalType, this.fromRectangle.sprinklersPara);
						}
						else if (parameter7 != null)
						{
							parameter7.Set(UnitConvert.CovertToAPI(this.fromRectangle.sprinklersPara.OffSet * 1000.0, (Autodesk.Revit.DB.DisplayUnitType)2));
						}
						CreateProtectRange createProtectRange = new CreateProtectRange();
						if (this.fromRectangle.sprinklersPara.Range && (int)this.doc.ActiveView.ViewType != 4)
						{
							List<Element> collection = new List<Element>
							{
								elementById
							};
							createProtectRange.CreatActiveViewDetailCurve(this.doc, collection, HyRangeCheckMajor.HyGPS);
						}
					}
					else if (this.layCom == SprinklerModelProcess.LaySprinklerType.Lay_Base)
					{
						Parameter parameter8 = (elementById as FamilyInstance).Symbol.GetParameter(BuiltInParameter.RBS_FP_SPRINKLER_K_FACTOR_PARAM);
						if (parameter8 != null)
						{
							parameter8.Set(80);
						}
						Parameter parameter9 = elementById.GetParameter("喷淋半径");
						if (parameter9 == null)
						{
							hyparametersSetting.SetSprinklersRudioShareParam();
							parameter9 = elementById.GetParameter("喷淋半径");
						}
						if (parameter9 != null)
						{
							parameter9.Set(UnitConvert.CovertToAPI(this.basementManage.layParameter.Radius * 1000.0, (Autodesk.Revit.DB.DisplayUnitType)2));
						}
						Parameter parameter10 = elementById.GetParameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM);
						if (parameter10 != null)
						{
							parameter10.Set(UnitConvert.CovertToAPI(this.basementManage.layParameter.Elevation * 1000.0, (Autodesk.Revit.DB.DisplayUnitType)2));
						}
					}
					this.doc.Regenerate();
				}
			}
			catch (Exception ex)
			{
				string message = ex.Message;
			}
			List<List<Element>> result = new List<List<Element>>();
			try
			{
				HYPipeConnect hypipeConnect = new HYPipeConnect();
				PipingSystemType pipingSystemType = null;
				double pipeDistance = 0.0;
				int num = 1;
				int index = 0;
				int index2 = 0;
				if (this.layCom == SprinklerModelProcess.LaySprinklerType.Lay_Rect)
				{
					pipeDistance = this.fromRectangle.sprinklersPara.PipeDistance;
					num = this.fromRectangle.familyConnStyle;
					index = this.fromRectangle.sprinklersPara.PipeType;
					if (bl)
					{
						index2 = this.fromRectangle.sprinklersPara.PipingSystemType;
					}
				}
				else if (this.layCom == SprinklerModelProcess.LaySprinklerType.Lay_Base)
				{
					pipeDistance = this.basementManage.layParameter.Distance;
					num = this.basementManage.layParameter.LinkType;
					index = this.basementManage.layParameter.PiptType;
					if (bl)
					{
						index2 = this.fromRectangle.sprinklersPara.PipingSystemType;
					}
				}
				List<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(PipingSystemType), this.doc);
				if (bl)
				{
					pipingSystemType = (elementByClass[index2] as PipingSystemType);
				}
				else
				{
					foreach (Element element in elementByClass)
					{
						pipingSystemType = (element as PipingSystemType);
						if ((int)pipingSystemType.SystemClassification == 23)
						{
							break;
						}
					}
				}
				PipeType pipeType = YJKElementFilter.GetElementByClass(typeof(PipeType), this.doc)[index] as PipeType;
				if (num == 1)
				{
					if (this.layCom == SprinklerModelProcess.LaySprinklerType.Lay_Rect)
					{
						Pipe pipe;
						if (this.fromRectangle.layTerminalType == 5)
						{
							pipe = Pipe.Create(this.doc, pipingSystemType.Id, pipeType.Id, this.doc.ActiveView.GenLevel.Id, new XYZ(0.0, -100.0, 0.0), new XYZ(0.0, 100.0, 0.0));
						}
						else if (bl)
						{
							YJKLine hyline = new YJKLine(this.startMin, this.startMax);
							if (Math.Round(this.startMin.X, 5) == Math.Round(this.startMax.X, 5))
							{
								hyline = new YJKLine(this.startMin, this.startMax);
								hyline.MakeUnBound();
								pipe = Pipe.Create(this.doc, pipingSystemType.Id, pipeType.Id, this.doc.ActiveView.GenLevel.Id, this.startEnd, hyline.GetProject(this.startEnd));
							}
							else if (Math.Round(this.startMin.Y, 5) == Math.Round(this.startMax.Y, 5))
							{
								hyline = new YJKLine(this.startMin, new XYZ(this.startMax.X, this.startMax.Y + 0.01, this.startMax.Z));
								hyline.MakeUnBound();
								pipe = Pipe.Create(this.doc, pipingSystemType.Id, pipeType.Id, this.doc.ActiveView.GenLevel.Id, this.startEnd, hyline.GetProject(this.startEnd));
							}
							else
							{
								hyline = new YJKLine(this.startMin, new XYZ(this.startMax.X + 0.01, this.startMax.Y, this.startMax.Z));
								hyline.MakeUnBound();
								pipe = Pipe.Create(this.doc, pipingSystemType.Id, pipeType.Id, this.doc.ActiveView.GenLevel.Id, this.startEnd, hyline.GetProject(this.startEnd));
							}
						}
						else
						{
							YJKLine hyline2 = new YJKLine(this.startMin, this.startMax);
							hyline2.MakeUnBound();
							pipe = Pipe.Create(this.doc, pipingSystemType.Id, pipeType.Id, this.doc.ActiveView.GenLevel.Id, this.startEnd, hyline2.GetProject(this.startEnd));
						}
						result = hypipeConnect.SprinklerConn(this.doc, list, pipeDistance, pipe);
					}
					else if (this.layCom == SprinklerModelProcess.LaySprinklerType.Lay_Base)
					{
						YJKLine hyline3 = new YJKLine(this.startMin, this.startMax);
						hyline3.MakeUnBound();
						Pipe pipe = Pipe.Create(this.doc, pipingSystemType.Id, pipeType.Id, this.doc.ActiveView.GenLevel.Id, this.startEnd, hyline3.GetProject(this.startEnd));
						result = hypipeConnect.SprinklerConn(this.doc, list, pipeDistance, pipe);
					}
				}
				if (num == 2)
				{
					if (this.layCom == SprinklerModelProcess.LaySprinklerType.Lay_Rect)
					{
						Pipe pipe2;
						if (this.fromRectangle.layTerminalType == 5)
						{
							pipe2 = Pipe.Create(this.doc, pipingSystemType.Id, pipeType.Id, this.doc.ActiveView.GenLevel.Id, new XYZ(-100.0, 0.0, 0.0), new XYZ(100.0, 0.0, 0.0));
						}
						else if (bl)
						{
							if (Math.Round(this.startMin.X, 5) == Math.Round(this.startMax.X, 5))
							{
								pipe2 = Pipe.Create(this.doc, pipingSystemType.Id, pipeType.Id, this.doc.ActiveView.GenLevel.Id, this.startMin, new XYZ(this.startMax.X + 0.01, this.startMin.Y, this.startMax.Z));
							}
							else if (Math.Round(this.startMin.Y, 5) == Math.Round(this.startMax.Y, 5))
							{
								pipe2 = Pipe.Create(this.doc, pipingSystemType.Id, pipeType.Id, this.doc.ActiveView.GenLevel.Id, this.startMin, this.startMax);
							}
							else
							{
								pipe2 = Pipe.Create(this.doc, pipingSystemType.Id, pipeType.Id, this.doc.ActiveView.GenLevel.Id, this.startMin, this.startMax);
							}
						}
						else
						{
							pipe2 = Pipe.Create(this.doc, pipingSystemType.Id, pipeType.Id, this.doc.ActiveView.GenLevel.Id, this.startMin, this.startMax);
						}
						result = hypipeConnect.SprinklerConn(this.doc, list, pipeDistance, pipe2);
					}
					else if (this.layCom == SprinklerModelProcess.LaySprinklerType.Lay_Base)
					{
						Pipe pipe2 = Pipe.Create(this.doc, pipingSystemType.Id, pipeType.Id, this.doc.ActiveView.GenLevel.Id, this.startMin, this.startMax);
						result = hypipeConnect.SprinklerConn(this.doc, list, pipeDistance, pipe2);
					}
				}
			}
			catch (Exception ex2)
			{
				string message2 = ex2.Message;
			}
			return result;
		}

		private bool ParkingIdent(List<SprinklerGroup> sgList)
		{
			RectangleRange rr = new RectangleRange(this.startMin, this.startMax, this.startEnd, this.startMin + (this.startEnd - this.startMax));
			List<Element> list = new FilteredElementCollector(this.doc).OfCategory(BuiltInCategory.OST_Parking).OfClass(typeof(FamilyInstance)).ToElements().ToList<Element>();
			List<Parking> list2 = new List<Parking>();
			double z = 0.0;
			if (this.layCom == SprinklerModelProcess.LaySprinklerType.Lay_Rect)
			{
				z = UnitConvert.CovertToAPI(this.fromRectangle.sprinklersPara.OffSet * 1000.0, (Autodesk.Revit.DB.DisplayUnitType)2);
			}
			else if (this.layCom == SprinklerModelProcess.LaySprinklerType.Lay_Base)
			{
				z = UnitConvert.CovertToAPI(this.basementManage.layParameter.Elevation * 1000.0, (Autodesk.Revit.DB.DisplayUnitType)2);
			}
			foreach (Element e in list)
			{
				Parking parking = new Parking(e, z, this.doc, null);
				if (parking.center != null && parking.rr != null)
				{
					list2.Add(parking);
				}
			}
			list2.AddRange(this.rsh.GetLinkDocPakings(z));
			if (list2.Count > 0)
			{
				double z2 = this.uiApp.ActiveUIDocument.ActiveView.GenLevel.Elevation - 0.32808398950131235;
				double z3 = this.uiApp.ActiveUIDocument.ActiveView.GenLevel.Elevation + 3.2808398950131235;
				if (this.floorrang.floorM == YJKViewRange.FloorMark.MidFloor && this.floorrang.height > 0.65616797900262469)
				{
					z3 = this.uiApp.ActiveUIDocument.ActiveView.GenLevel.Elevation + this.floorrang.height - 0.32808398950131235;
				}
				list2 = this.rsh.GetInnerParkings(list2, rr, z2, z3);
				List<SprinklerGroup> list3 = new List<SprinklerGroup>();
				list3.AddRange(sgList);
				List<Parking> unSprayedPakings = this.rsh.GetUnSprayedPakings(list2, list3);
				List<Parking> list4 = new List<Parking>();
				foreach (Parking parking2 in unSprayedPakings)
				{
					bool flag = false;
					using (List<Parking>.Enumerator enumerator3 = list4.GetEnumerator())
					{
						while (enumerator3.MoveNext())
						{
							if (enumerator3.Current == parking2)
							{
								flag = true;
								break;
							}
						}
					}
					if (!flag)
					{
						if (list3.Count <= 0)
						{
							break;
						}
						if (this.layCom == SprinklerModelProcess.LaySprinklerType.Lay_Rect)
						{
							list4.AddRange(this.rsh.MoveSGOnParking(parking2, list3, sgList, unSprayedPakings, (double)this.fromRectangle.sprinklersPara.BeiShu / 304.8, this.fromRectangle.layAreaParameter.RowDistance, this.fromRectangle.layAreaParameter.LessDistance));
						}
						else if (this.layCom == SprinklerModelProcess.LaySprinklerType.Lay_Base)
						{
							list4.AddRange(this.rsh.MoveSGOnParking(parking2, list3, sgList, unSprayedPakings, (double)this.basementManage.GetRectParater().BeiShu / 304.8, this.basementManage.GetRectParater().YLength, this.basementManage.GetRectParater().LessDistance));
						}
					}
				}
			}
			return true;
		}

		private void GetApartBeam(List<SprinklerGroup> sgList, List<BeamModel> beams)
		{
			if (sgList == null || sgList.Count <= 0 || beams == null || beams.Count <= 0)
			{
				return;
			}
			foreach (SprinklerGroup sprinklerGroup in sgList)
			{
				sprinklerGroup.beamList.Clear();
				foreach (BeamModel beamModel in beams)
				{
					if (beamModel.IsParallel && this.IsInSaveDis(sprinklerGroup, beamModel) && this.BeamUnderFloor(beamModel))
					{
						beamModel.GetBeamSize(ref beamModel.Width, ref beamModel.Height);
						sprinklerGroup.beamList.Add(beamModel);
					}
				}
			}
		}

		private bool BeamUnderFloor(BeamModel beam)
		{
			bool result;
			try
			{
				double num = beam.Elevation - beam.Height / 2.0;
				foreach (Element element in this.GetAllConnectElements(beam))
				{
					if (element is Floor)
					{
						BoundingBoxXYZ boundingBoxXYZ = (element as Floor).get_BoundingBox(this.doc.ActiveView);
						if (((boundingBoxXYZ.Max.Z < boundingBoxXYZ.Min.Z) ? boundingBoxXYZ.Max.Z : boundingBoxXYZ.Min.Z) - num > 0.65616797900262469)
						{
							return true;
						}
					}
				}
				result = false;
			}
			catch
			{
				result = false;
			}
			return result;
		}

		private bool IsInSaveDis(SprinklerGroup sg, BeamModel beam)
		{
			double num = this.SprinklerDisBeam + beam.Width / 2.0;
			foreach (XYZ xyz in sg.sprinklerPosition)
			{
				XYZ xyz2 = new XYZ(xyz.X, xyz.Y, 0.0);
				YJKLine hyline = new YJKLine(new XYZ(beam.systemLine.Tessellate()[0].X, beam.systemLine.Tessellate()[0].Y, 0.0), new XYZ(beam.systemLine.Tessellate()[1].X, beam.systemLine.Tessellate()[1].Y, 0.0));
				XYZ project = hyline.GetProject(xyz2);
				if (project != null && hyline.IsPointOnLine(project) && project.DistanceTo(xyz2) < num)
				{
					return true;
				}
			}
			return false;
		}

		private bool BeamAside(List<SprinklerGroup> sgList)
		{
			new RectangleRange(this.startMin, this.startMax, this.startEnd, this.startMin + (this.startEnd - this.startMax));
			double elevation = this.uiApp.ActiveUIDocument.ActiveView.GenLevel.Elevation;
			List<BeamModel> allBeams = this.rsh.GetAllBeams(this.floorrang, elevation);
			this.GetApartBeam(sgList, allBeams);
			foreach (SprinklerGroup sprinklerGroup in sgList)
			{
				if (sprinklerGroup.beamList.Count > 0)
				{
					if (this.layCom == SprinklerModelProcess.LaySprinklerType.Lay_Rect)
					{
						this.rsh.MoveBeamSprinklerGroup(sprinklerGroup, sgList, this.fromRectangle.layAreaParameter.RowDistance, this.fromRectangle.layAreaParameter.LessDistance, (double)this.fromRectangle.sprinklersPara.BeiShu / 304.8, this.SprinklerDisBeam);
					}
					else if (this.layCom == SprinklerModelProcess.LaySprinklerType.Lay_Base)
					{
						this.rsh.MoveBeamSprinklerGroup(sprinklerGroup, sgList, this.basementManage.GetRectParater().YSpace, this.basementManage.GetRectParater().LessDistance, (double)this.basementManage.GetRectParater().BeiShu / 304.8, this.SprinklerDisBeam);
					}
				}
			}
			return true;
		}

		private bool PillarAside(List<SprinklerGroup> sgList)
		{
			RectangleRange rectRange = new RectangleRange(this.startMin, this.startMax, this.startEnd, this.startMin + (this.startEnd - this.startMax));
			double elevation = this.uiApp.ActiveUIDocument.ActiveView.GenLevel.Elevation;
			List<ColumnModel> cmList = new List<ColumnModel>();
			cmList = this.rsh.GetAllColumns(elevation, this.floorrang, rectRange);
			List<SprinklerGroup> list = new List<SprinklerGroup>();
			list = this.rsh.GetConflictSGList(sgList, cmList);
			if (list.Count > 0)
			{
				foreach (SprinklerGroup sg in list)
				{
					if (this.layCom == SprinklerModelProcess.LaySprinklerType.Lay_Rect)
					{
						if (!this.rsh.MoveAwaySprinklerGroup(sg, sgList, this.fromRectangle.layAreaParameter.RowDistance, this.fromRectangle.layAreaParameter.LessDistance, (double)this.fromRectangle.sprinklersPara.BeiShu / 304.8))
						{
							this.rsh.SystemDevation(sg);
						}
					}
					else if (this.layCom == SprinklerModelProcess.LaySprinklerType.Lay_Base && !this.rsh.MoveAwaySprinklerGroup(sg, sgList, this.basementManage.GetRectParater().YSpace, this.basementManage.GetRectParater().LessDistance, (double)this.basementManage.GetRectParater().BeiShu / 304.8))
					{
						this.rsh.SystemDevation(sg);
					}
				}
			}
			return true;
		}

		private CurveLoop GetCurveLoopFromBeam(BeamModel beam)
		{
			CurveLoop result;
			try
			{
				CurveLoop curveLoop = new CurveLoop();
				XYZ xyz = beam.systemLine.Tessellate()[0];
				XYZ xyz2 = beam.systemLine.Tessellate()[1];
				XYZ xyz3 = beam.faceOri.Normalize();
				double num = beam.Elevation;
				num -= beam.Height / 2.0;
				XYZ xyz4 = new XYZ((xyz + beam.Width * xyz3).X, (xyz + beam.Width * xyz3).Y, num);
				XYZ xyz5 = new XYZ((xyz2 + beam.Width * xyz3).X, (xyz2 + beam.Width * xyz3).Y, num);
				XYZ xyz6 = new XYZ((xyz2 - beam.Width * xyz3).X, (xyz2 - beam.Width * xyz3).Y, num);
				XYZ xyz7 = new XYZ((xyz - beam.Width * xyz3).X, (xyz - beam.Width * xyz3).Y, num);
				curveLoop.Append(Line.CreateBound(xyz4, xyz5));
				curveLoop.Append(Line.CreateBound(xyz5, xyz6));
				curveLoop.Append(Line.CreateBound(xyz6, xyz7));
				curveLoop.Append(Line.CreateBound(xyz7, xyz4));
				result = curveLoop;
			}
			catch
			{
				result = null;
			}
			return result;
		}

		private List<Element> GetAllConnectElements(BeamModel beam)
		{
			IEnumerable<LinkedDocument> linkedDocuments = this.doc.GetLinkedDocuments();
			CurveLoop curveLoopFromBeam = this.GetCurveLoopFromBeam(beam);
			List<Element> typeElements = this.GetTypeElements(this.doc, curveLoopFromBeam, beam.Height, BuiltInCategory.OST_Floors);
			foreach (LinkedDocument linkedDocument in linkedDocuments)
			{
				Transform transform = Transform.CreateTranslation(linkedDocument.Transform.Origin * -1.0);
				CurveLoop arCurveLoop = CurveLoop.CreateViaTransform(curveLoopFromBeam, transform);
				typeElements.AddRange(this.GetTypeElements(linkedDocument.Document, arCurveLoop, beam.Height, BuiltInCategory.OST_Floors));
			}
			return typeElements;
		}

		private List<Element> GetTypeElements(Autodesk.Revit.DB.Document docCur, CurveLoop arCurveLoop, double extrusionDist, BuiltInCategory inCategory)
		{
			List<Element> list = new List<Element>();
			if (arCurveLoop == null)
			{
				return list;
			}
			Solid solid = null;
			try
			{
				solid = GeometryCreationUtilities.CreateExtrusionGeometry(new List<CurveLoop>
				{
					arCurveLoop
				}, XYZ.BasisZ, extrusionDist);
			}
			catch
			{
			}
			ElementIntersectsSolidFilter elementIntersectsSolidFilter = new ElementIntersectsSolidFilter(solid);
			foreach (Element item in new FilteredElementCollector(docCur).OfCategory(inCategory).WherePasses(elementIntersectsSolidFilter).ToElements().ToList<Element>())
			{
				list.Add(item);
			}
			return list;
		}

		private SprinklerModelProcess.LaySprinklerType layCom;

		private double SprinklerDisBeam = 1.9685039370078738;

		private YJKWindowHandle _wndRevit;

		private HYEquipmentCommon equipmentCM = new HYEquipmentCommon();

		private FamilySymbol familySymbol;

		private SpaceFamilyDataReader sfdr = new SpaceFamilyDataReader();

		private RecSprinklerHelper rsh;

		private LaySprinklerHelper lph;

		private UIApplication uiApp;

		private Document doc;

		private YJKViewRange floorrang;

		private List<XYZ> listPoint;

		private XYZ startMin;

		private XYZ startMax;

		private XYZ startEnd;

		private LayBasementManage basementManage;

		private FormLaySprinklerByRectangle fromRectangle;

		private enum LaySprinklerType
		{
			Lay_Rect,
			Lay_Base
		}
	}
}
